package com.dldata.devtools30.controller.sysform;


import com.dldata.devtools30.entity.DevtFormDesignEntity;
import com.dldata.devtools30.entity.DevtFormListtemplateEntity;
import com.dldata.devtools30.entity.DevtFormMetadataEntity;
import com.dldata.devtools30.entity.DevtModelMetadataTableEntity;
import com.dldata.devtools30.repository.metadata.DevtModelMetadataTableDao;
import com.dldata.devtools30.repository.sysform.DevtFormDesignEntityDao;
import com.dldata.devtools30.repository.sysform.DevtFormListtemplateEntityDao;
import com.dldata.devtools30.repository.sysform.DevtFormMetadataDao;
import com.dldata.devtools30.utils.StringUtil;
import com.google.common.collect.Lists;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**

 */
@Controller
@RequestMapping(value = {"/V_DevtFormDataController","/FormApi30"})
public class V_DevtFormDataController {

    @Autowired
    DevtFormDesignEntityDao formDesignDao;
    @Autowired
    DevtFormMetadataDao sysCrfFormMetadataDao;
    @Autowired
    DevtModelMetadataTableDao metadataTableDao;
    @Autowired
    DevtFormListtemplateEntityDao sysCrfFormListtemplateEntityDao;

    /**
     * @param formId
     * @param dataId
     * @param formStatus
     * @param mDataId    父表到子表添加页面时需要
     * @param model
     * @param param
     * @return
     * http://127.0.0.1/V_DevtFormDataController/toEdit?formId=3c8df8dc-31bc-443e-83ea-cad76e744817&mDataId=&dataId=3&mobanId=&formStatus=
     * /V_DevtFormDataController/toEdit?formId=ba1f0de9-3daa-41d0-a08d-17af41716382&mDataId=&dataId=74cf157a-86a7-4038-8f0d-a9eeb2801d3f
     * /V_DevtFormDesignEntityController/saveAndUpdatePreview?tempid=024ee474-9771-4a10-904d-ea14dd95f25d&callback=btnParentSearch&dataid=20fb2036-fe5f-460d-9273-d49c3bd6cc92
     */
    @RequestMapping(value = {"/toEdit",""})
    public void toEdit(@RequestParam(required = false)String formId, @RequestParam(required = false)final String dataId
            , @RequestParam(required = false)String mobanId, @RequestParam(required = false) String formStatus,
                         @RequestParam(required = false)String mDataId, Model model,
                         @RequestParam Map<String, Object> param, HttpServletRequest request, HttpServletResponse response) {
        DevtFormListtemplateEntity sysCrfFormListtemplateEntity =null;
        if(!StringUtil.isEmptyOrLength0(mobanId)){
            sysCrfFormListtemplateEntity = sysCrfFormListtemplateEntityDao.findById(mobanId).get();
            sysCrfFormListtemplateEntity=(sysCrfFormListtemplateEntity!=null&&formDesignDao.getFormDesignDataByTempid(sysCrfFormListtemplateEntity.getId()).size()>0)?sysCrfFormListtemplateEntity:null;
        }
        if(sysCrfFormListtemplateEntity==null&&!StringUtil.isEmptyOrLength0(formId)){
            DevtFormMetadataEntity sysCrfFormMetadataEntity1 = Lists.newArrayList(sysCrfFormMetadataDao.findAll()).stream().filter(
                    sysCrfFormMetadataEntity -> {
                        return sysCrfFormMetadataEntity.getTableid().equalsIgnoreCase(formId) && sysCrfFormMetadataEntity.getTabletype().equalsIgnoreCase("父表");
                    }
            ).findFirst().orElse(null);
            if(sysCrfFormMetadataEntity1!=null){
                sysCrfFormListtemplateEntity = sysCrfFormListtemplateEntityDao.findById(sysCrfFormMetadataEntity1.getTempleid()).get();
                sysCrfFormListtemplateEntity=(sysCrfFormListtemplateEntity!=null&&formDesignDao.getFormDesignDataByTempid(sysCrfFormListtemplateEntity.getId()).size()>0)?sysCrfFormListtemplateEntity:null;
            }
        }
        if(sysCrfFormListtemplateEntity==null){
            try {
                response.getWriter().println("Templeid is null.");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        String templateid=sysCrfFormListtemplateEntity.getId();
        DevtFormMetadataEntity sysCrfFormMetadataEntity1 = Lists.newArrayList(sysCrfFormMetadataDao.findAll()).stream().filter(
                sysCrfFormMetadataEntity -> {
                    return sysCrfFormMetadataEntity.getTempleid().equalsIgnoreCase(templateid) && sysCrfFormMetadataEntity.getTabletype().equalsIgnoreCase("父表");
                }
        ).findFirst().orElse(null);
        DevtModelMetadataTableEntity tableEntity = metadataTableDao.findById(sysCrfFormMetadataEntity1.getTableid()).get();
        String dispatcherUrl="/V_DevtFormDesignEntityController/saveAndUpdatePreview?1=1" ;
        dispatcherUrl=dispatcherUrl+"&tempid="+sysCrfFormListtemplateEntity.getId();
        if(StringUtil.isEmptyOrLength0(mDataId)){
            dispatcherUrl=dispatcherUrl+"&mDefaultValue_mid="+sysCrfFormListtemplateEntity.getId();
            dispatcherUrl=dispatcherUrl+"&mDefaultValue_MID="+sysCrfFormListtemplateEntity.getId();
        }
        Set<String> parameterKey= param.keySet();
        for (Iterator<String> iterator = parameterKey.iterator(); iterator.hasNext(); ) {
            String next = iterator.next();
            if(next.startsWith("mDefaultValue_")){
                String key="mDefaultValue_"+tableEntity.getCode()+"@"+next.replace("mDefaultValue_","");
                dispatcherUrl=dispatcherUrl+"&"+key+"="+param.get(next).toString();
            }
        }
        dispatcherUrl=dispatcherUrl+"&dataid="+dataId;
        try {
            request.getRequestDispatcher(dispatcherUrl).forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     *
     * @param formId
     * @param formStatus
     * @param model
     * @return
     */
    @RequestMapping(value = "/toList")
    public void toList(@RequestParam(required = false)String formId,@RequestParam(required = false) String formStatus, Model model,
                         @RequestParam Map<String, String> param, HttpServletRequest request, HttpServletResponse response) {
        String mobanId=param.get("mobanId");
        DevtFormDesignEntity formDesignDaoOne =null;
        if(!StringUtil.isEmptyOrLength0(mobanId)){
            formDesignDaoOne = formDesignDao.findById(mobanId).get();
        }
        if(formDesignDaoOne==null&&!StringUtil.isEmptyOrLength0(formId)){
            DevtFormMetadataEntity sysCrfFormMetadataEntity1 =Lists.newArrayList( sysCrfFormMetadataDao.findAll()).stream().filter(
                    sysCrfFormMetadataEntity -> {
                        return sysCrfFormMetadataEntity.getTableid().equalsIgnoreCase(formId) && sysCrfFormMetadataEntity.getTabletype().equalsIgnoreCase("父表");
                    }
            ).findFirst().orElse(null);
            if(sysCrfFormMetadataEntity1!=null){
                formDesignDaoOne = formDesignDao.findById(sysCrfFormMetadataEntity1.getTempleid()).get();
            }
        }
        if(formDesignDaoOne==null){
            try {
                response.getWriter().println("Templeid is null.");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        String templateid=formDesignDaoOne.getTemplateid();
        String dispatcherUrl="/V_SysFromPreviewController/toDldsnformPreview?1=1" ;
        dispatcherUrl=dispatcherUrl+"&tempid="+templateid;
        try {
            request.getRequestDispatcher(dispatcherUrl).forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
